package com.cloudwise.gproject.controller;

import com.cloudwise.gproject.entity.LayUiTree;
import com.cloudwise.gproject.entity.Menu;
import com.cloudwise.gproject.entity.Role;
import com.cloudwise.gproject.entity.User;
import com.cloudwise.gproject.service.MenuService;
import com.cloudwise.gproject.service.RoleService;
import com.cloudwise.gproject.util.Result;
import com.cloudwise.gproject.util.ReturnStatus;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色信息表(Role)表控制层
 *
 * @author makejava
 * @since 2021-03-23 18:59:18
 */
@Controller
@RequestMapping("role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;
    @Resource
    private MenuService menuService;

    @ResponseBody
    @GetMapping("findSelectMenu")
    public List<Integer> findSelectMenu(int roleId) {
        System.out.println("++++++++++++++findSelectMenu+++++++++++++++");

        return roleService.findSelectMenu(roleId);
    }


    /**
     * @param :
     * @return java.lang.Object
     * @create by: IvanZ
     * @description : 获取树形菜单数据
     * @create time: 2020/12/20 1:12
     */
    @ResponseBody
    @GetMapping("findAllMenu")
    public List<LayUiTree> findAllMenu() {
        List<Menu> menus = menuService.queryAll(new Menu());

        List<LayUiTree> list = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId() == 0) {
                LayUiTree tree = new LayUiTree();
                tree.setId(menu.getMenuId());
                tree.setTitle(menu.getMenuName());
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                //组件展开
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                list.add(tree);
            }
        }
        return list;
    }

    /**
     * @param parentId:
     * @param menus:
     * @return java.util.List<com.cloudwise.trademark.entity.LayUiTree>
     * @create by: IvanZ
     * @description : 递归得到孩子节点
     * @create time: 2020/12/20 1:12
     */
    public List<LayUiTree> getChildren(Integer parentId, List<Menu> menus) {
        List<LayUiTree> list = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId().equals(parentId)) {
                LayUiTree tree = new LayUiTree();
                tree.setId(menu.getMenuId());
                tree.setTitle(menu.getMenuName());
                //组件展开
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                list.add(tree);
            }
        }
        return list;
    }

    /** 
     * @create by: 卫子健
     * @description: 删除多个id对应的数据 
     * @create time: 2021/3/19 23:21
     * @param roleIds 
     * @return  
     */
    @ResponseBody
    @RequestMapping("deleteByIds")
    public Result deleteByIds(String roleIds){
        int i = roleService.deleteByIds(roleIds);
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0,i);
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @ResponseBody
    @RequestMapping("selectOne")
    public Result selectOne(Integer id) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.roleService.queryById(id));
    }

    /**
     * 跳转部门分页列表页面
     * @return
     */
    @RequestMapping("toRole")
    public String toRole() {
        return "back/role";
    }

    /**
     * 通过实体作为筛选条件查询
     * @return 对象列表
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page, Integer limit, Role role) {
        PageInfo pageInfo = roleService.queryAll(page, limit, role);
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增数据
     * @param role 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("insert")
    public Result insert(Role role, String menus) {
        Role insert = roleService.insert(role);
        if (insert!=null) {
            String[] menusId = menus.split(",");
            roleService.insertRoleMenu(insert.getRoleId(), menusId);
            return new Result(ReturnStatus.SUCCESS.getCode(),
                    ReturnStatus.SUCCESS.getMessage(), 0, insert);
        }
        return new Result(ReturnStatus.FAIL.getCode(),
                ReturnStatus.FAIL.getMessage(), 0, null);
    }

    /** 
     * @create by: 卫子健
     * @description: 更新单个数据 
     * @create time: 2021/3/19 21:44
     * @param  
     * @return  
     */
    @ResponseBody
    @PutMapping("update")
    public Result update(Role role, String menus, HttpSession session){
        //获取当前用户的用户名即为 执行了更新操作的人
        User userInfo = (User)session.getAttribute("userInfo");
        role.setUpdateBy(userInfo.getUserName());

        Role update = roleService.update(role);
        if (update != null) {
            String[] menusId = menus.split(",");
            roleService.updateRoleMenu(update.getRoleId(), menusId);
            return new Result(ReturnStatus.SUCCESS.getCode(),
                    ReturnStatus.SUCCESS.getMessage(), 0, update);
        }
        return new Result(ReturnStatus.FAIL.getCode(),
                ReturnStatus.FAIL.getMessage(), 0, null);
    }

    /**
     * 通过主键删除数据
     * @param roleId 主键
     * @return 是否成功
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public Result deleteById(Integer roleId) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.roleService.deleteById(roleId));
    }
}
